layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'vip/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: 'VIP用户列表',
        height: globalHeight,
        page: true,
        cols: [[
            {type: 'checkbox'},
            {field: 'userId', title: '用户id', width: 150, align: 'center'},
            {field: 'name', title: '昵称', align: 'center'},
            {field: 'icon', title: '头像', align: 'center', width: 100, templet: '#iconTpl'},
            {field: 'identity', title: '身份', width: 100, align: 'center'},
            {field: 'beginTime', title: '开始时间', width: 200, align: 'center'},
            {field: 'endTime', title: '结束时间', width: 200, align: 'center'},
            {field: 'createTime', title: '创建时间', width: 250, align: 'center'},
            {field: 'op', title: '操作', align: 'center', width: 200, templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.vipUsers //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        var checks = table.checkStatus(obj.config.id);
        var data = checks.data;
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'vip/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
            case 'add':
                layer.open({
                    type: 2,
                    title: '添加会员',
                    offset: '20%',
                    content: 'toVipAdd',
                    area: ['500px', '280px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {     //确定按钮回调
                        var iframeWindow = window['layui-layer-iframe'+ index],
                            submit = layero.find('iframe').contents().find("#data-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(data-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            post('vip/add', field, field, function (data) {
                                layer.msg("添加成功");
                                table.reload('data-table');
                                layer.close(index); //关闭弹层
                            });
                        });

                        submit.trigger('click');
                    }
                });
                break;
            case 'del':
                if(data.length <= 0){
                    layer.msg("请选择要删除的会员！");
                    return;
                }
                layer.confirm('确定删除'+data.length+'个会员', {offset: '30%'}, function(index){
                    var ids = getIds(data, 'id');
                    // 加载层
                    var load = layerLoad();
                    post('vip/del', {'ids': ids}, false, function (data) {
                        table.reload('data-table');
                        layer.msg("删除成功");
                        layer.close(index);
                        layer.close(load);
                    });
                });
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('真的删除该会员', {offset: '30%'}, function(index){
                var ids = [];
                ids.push(data.id);
                // 加载层
                var load = layerLoad();
                post('vip/del', {'ids': ids}, false, function (data) {
                    obj.del();
                    layer.msg("删除成功");
                    layer.close(index);
                    layer.close(load);
                });
            });
        }
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var keyword = $('#keyword').val();
        var time = $('#time').val();

        return {'keyword': keyword, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#keyword').val(data.keyword);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});